

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Moe Wang">
  <meta name="keywords" content="">
  
    <meta name="description" content="前言在之前的文章中，我们大部分使用的添加实体的方式都是entity添加，而在性能优化中我们提到了primitive添加的方式，entity在本文就不多做介绍了，本文主要介绍一下primitive相关知识。 什么是Primitive首先我们在Cesium API文档中搜索primitive，看文档中对Primitive类的介绍，翻译过来大致如下：  图元代表场景中的几何体。 几何可以来自单个 Geo">
<meta property="og:type" content="article">
<meta property="og:title" content="Cesium | Primitive图元介绍及与Entity对比">
<meta property="og:url" content="https://moewang0321.github.io/2021/06/17/Cesium%E4%B8%ADPrimitive%E5%9B%BE%E5%85%83%E4%BB%8B%E7%BB%8D%E5%8F%8A%E4%B8%8EEntity%E5%AF%B9%E6%AF%94/index.html">
<meta property="og:site_name" content="moe_大杂烩">
<meta property="og:description" content="前言在之前的文章中，我们大部分使用的添加实体的方式都是entity添加，而在性能优化中我们提到了primitive添加的方式，entity在本文就不多做介绍了，本文主要介绍一下primitive相关知识。 什么是Primitive首先我们在Cesium API文档中搜索primitive，看文档中对Primitive类的介绍，翻译过来大致如下：  图元代表场景中的几何体。 几何可以来自单个 Geo">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-06-17T02:58:55.000Z">
<meta property="article:modified_time" content="2023-02-14T08:48:35.352Z">
<meta property="article:author" content="Moe Wang">
<meta property="article:tag" content="JavaScript">
<meta property="article:tag" content="Cesium">
<meta name="twitter:card" content="summary_large_image">
  
  
  
  <title>Cesium | Primitive图元介绍及与Entity对比 - moe_大杂烩</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"moewang0321.github.io","root":"/","version":"1.9.4","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 5.4.2"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>moe_大杂烩</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                <span>分类</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                <span>标签</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                <span>关于</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="Cesium | Primitive图元介绍及与Entity对比"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2021-06-17 10:58" pubdate>
          2021年6月17日 上午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          5.5k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          47 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Cesium | Primitive图元介绍及与Entity对比</h1>
            
            
              <div class="markdown-body">
                
                <h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>在之前的文章中，我们大部分使用的添加实体的方式都是entity添加，而在性能优化中我们提到了primitive添加的方式，entity在本文就不多做介绍了，本文主要介绍一下primitive相关知识。</p>
<h2 id="什么是Primitive"><a href="#什么是Primitive" class="headerlink" title="什么是Primitive"></a>什么是Primitive</h2><p>首先我们在Cesium API文档中搜索primitive，看文档中对Primitive类的介绍，翻译过来大致如下：</p>
<blockquote>
<p>图元代表场景中的几何体。 几何可以来自单个 GeometryInstance，也可以来自实例数组，即使geometry 来自不同的几何类型。图元将geometry 实例与描述完整着色的 Appearance 相结合，包括 Material 和 RenderState。 粗略地说，geometry 实例定义了结构和位置，appearance 定义了视觉特征。 解耦geometry 和appearance 允许我们混合和匹配它们中的大部分，并相互独立地添加新的geometry 或appearance 。</p>
<p>将多个实例组合成一个原语称为批处理，可显着提高静态数据的性能。 实例可以单独挑选； Scene#pick 返回它们的 GeometryInstance#id。 使用 PerInstanceColorAppearance 等每个实例的外观，每个实例也可以具有唯一的颜色。</p>
<p><code>Geometry</code>可以在 web worker 或主线程上创建和批处理。</p>
</blockquote>
<p>通过阅读我们可以知道，primitive主要由两部分组成：Geometry和Appearance。</p>
<p>Geometry主要定义了primitive的几何结构，而Appearance主要负责定义primitive的着色，包括GLSL顶点着色器、片段着色器和渲染状态。</p>
<p>我们可以通过Primitive API来操控几何图形及其外观，绘制各种形状。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">var</span> instance = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Cesium</span>.<span class="hljs-title class_">GeometryInstance</span>(&#123;<br>  geometry : <span class="hljs-keyword">new</span> <span class="hljs-title class_">Cesium</span>.<span class="hljs-title class_">EllipseGeometry</span>(&#123;<br>      center : <span class="hljs-title class_">Cesium</span>.<span class="hljs-property">Cartesian3</span>.<span class="hljs-title function_">fromDegrees</span>(-<span class="hljs-number">100.0</span>, <span class="hljs-number">20.0</span>),<br>      semiMinorAxis : <span class="hljs-number">500000.0</span>,<br>      semiMajorAxis : <span class="hljs-number">1000000.0</span>,<br>      rotation : <span class="hljs-title class_">Cesium</span>.<span class="hljs-property">Math</span>.<span class="hljs-property">PI_OVER_FOUR</span>,<br>      vertexFormat : <span class="hljs-title class_">Cesium</span>.<span class="hljs-property">VertexFormat</span>.<span class="hljs-property">POSITION_AND_ST</span><br>  &#125;),<br>  id : <span class="hljs-string">&#x27;object returned when this instance is picked and to get/set per-instance attributes&#x27;</span><br>&#125;);<br>scene.<span class="hljs-property">primitives</span>.<span class="hljs-title function_">add</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Cesium</span>.<span class="hljs-title class_">Primitive</span>(&#123;<br>  geometryInstances : instance,<br>  appearance : <span class="hljs-keyword">new</span> <span class="hljs-title class_">Cesium</span>.<span class="hljs-title class_">EllipsoidSurfaceAppearance</span>(&#123;<br>    material : <span class="hljs-title class_">Cesium</span>.<span class="hljs-property">Material</span>.<span class="hljs-title function_">fromType</span>(<span class="hljs-string">&#x27;Checkerboard&#x27;</span>)<br>  &#125;)<br>&#125;));<br></code></pre></td></tr></table></figure>

<h3 id="Geometry"><a href="#Geometry" class="headerlink" title="Geometry"></a>Geometry</h3><p>在Cesium中，支持以下几种Geometry几何图形</p>
<table>
<thead>
<tr>
<th>几何图形</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>BoxGeometry</td>
<td>立方体</td>
</tr>
<tr>
<td>BoxOutlineGeometry</td>
<td>仅有轮廓的立方体，只有外部线条的的盒子</td>
</tr>
<tr>
<td>CircleGeometry</td>
<td>圆形或者拉伸的圆形，圆圈或挤压圆</td>
</tr>
<tr>
<td>CircleOutlineGeometry</td>
<td>只有轮廓的圆形</td>
</tr>
<tr>
<td>CorridorGeometry</td>
<td>走廊：沿着地表的多段线(垂直于表面的折线)，且具有一定的宽度，可以拉伸到一定的高度</td>
</tr>
<tr>
<td>CorridorOutlineGeometry</td>
<td>只有轮廓的走廊</td>
</tr>
<tr>
<td>CylinderGeometry</td>
<td>圆柱、圆锥或者截断的圆锥</td>
</tr>
<tr>
<td>CylinderOutlineGeometry</td>
<td>只有轮廓的圆柱、圆锥或者截断的圆锥</td>
</tr>
<tr>
<td>EllipseGeometry</td>
<td>椭圆或者拉伸的椭圆</td>
</tr>
<tr>
<td>EllipseOutlineGeometry</td>
<td>只有轮廓的椭圆或者拉伸的椭圆</td>
</tr>
<tr>
<td>EllipsoidGeometry</td>
<td>椭球体</td>
</tr>
<tr>
<td>EllipsoidOutlineGeometry</td>
<td>只有轮廓的椭球体</td>
</tr>
<tr>
<td>RectangleGeometry</td>
<td>矩形或者拉伸的矩形</td>
</tr>
<tr>
<td>RectangleOutlineGeometry</td>
<td>只有轮廓的矩形或者拉伸的矩形</td>
</tr>
<tr>
<td>PolygonGeometry</td>
<td>多边形，可以具有空洞或者拉伸一定的高度</td>
</tr>
<tr>
<td>PolygonOutlineGeometry</td>
<td>只有轮廓的多边形</td>
</tr>
<tr>
<td>PolylineGeometry</td>
<td>多段线，可以具有一定的宽度</td>
</tr>
<tr>
<td>SimplePolylineGeometry</td>
<td>简单的多段线</td>
</tr>
<tr>
<td>PolylineVolumeGeometry</td>
<td>多段线柱体</td>
</tr>
<tr>
<td>PolylineVolumeOutlineGeometry</td>
<td>只有轮廓的多段线柱体</td>
</tr>
<tr>
<td>SphereGeometry</td>
<td>球体</td>
</tr>
<tr>
<td>SphereOutlineGeometry</td>
<td>只有轮廓的球体</td>
</tr>
<tr>
<td>WallGeometry</td>
<td>墙</td>
</tr>
<tr>
<td>WallOutlineGeometry</td>
<td>只有轮廓的墙</td>
</tr>
</tbody></table>
<h4 id="Geometry-Instances-几何图形实例"><a href="#Geometry-Instances-几何图形实例" class="headerlink" title="Geometry Instances - 几何图形实例"></a>Geometry Instances - 几何图形实例</h4><p>在前边的示例代码中，我们已经用到了它，它相当于Geometry的容器，而多个Instance可以公用一个Geomotry并利用<code>GeometryInstance.modelMatrix</code>提供多种属性信息。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">var</span> geometry = <span class="hljs-title class_">Cesium</span>.<span class="hljs-property">BoxGeometry</span>.<span class="hljs-title function_">fromDimensions</span>(&#123;<br>  vertexFormat : <span class="hljs-title class_">Cesium</span>.<span class="hljs-property">VertexFormat</span>.<span class="hljs-property">POSITION_AND_NORMAL</span>,<br>  dimensions : <span class="hljs-keyword">new</span> <span class="hljs-title class_">Cesium</span>.<span class="hljs-title class_">Cartesian3</span>(<span class="hljs-number">1000000.0</span>, <span class="hljs-number">1000000.0</span>, <span class="hljs-number">500000.0</span>)<br>&#125;);<br><span class="hljs-keyword">var</span> instanceBottom = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Cesium</span>.<span class="hljs-title class_">GeometryInstance</span>(&#123;<br>  geometry : geometry,<br>  modelMatrix : <span class="hljs-title class_">Cesium</span>.<span class="hljs-property">Matrix4</span>.<span class="hljs-title function_">multiplyByTranslation</span>(<span class="hljs-title class_">Cesium</span>.<span class="hljs-property">Transforms</span>.<span class="hljs-title function_">eastNorthUpToFixedFrame</span>(<br>    <span class="hljs-title class_">Cesium</span>.<span class="hljs-property">Cartesian3</span>.<span class="hljs-title function_">fromDegrees</span>(-<span class="hljs-number">75.59777</span>, <span class="hljs-number">40.03883</span>)), <span class="hljs-keyword">new</span> <span class="hljs-title class_">Cesium</span>.<span class="hljs-title class_">Cartesian3</span>(<span class="hljs-number">0.0</span>, <span class="hljs-number">0.0</span>, <span class="hljs-number">1000000.0</span>), <span class="hljs-keyword">new</span> <span class="hljs-title class_">Cesium</span>.<span class="hljs-title class_">Matrix4</span>()),<br>  attributes : &#123;<br>    color : <span class="hljs-title class_">Cesium</span>.<span class="hljs-property">ColorGeometryInstanceAttribute</span>.<span class="hljs-title function_">fromColor</span>(<span class="hljs-title class_">Cesium</span>.<span class="hljs-property">Color</span>.<span class="hljs-property">AQUA</span>)<br>  &#125;,<br>  id : <span class="hljs-string">&#x27;bottom&#x27;</span><br>&#125;);<br><span class="hljs-keyword">var</span> instanceTop = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Cesium</span>.<span class="hljs-title class_">GeometryInstance</span>(&#123;<br>  geometry : geometry,<br>  modelMatrix : <span class="hljs-title class_">Cesium</span>.<span class="hljs-property">Matrix4</span>.<span class="hljs-title function_">multiplyByTranslation</span>(<span class="hljs-title class_">Cesium</span>.<span class="hljs-property">Transforms</span>.<span class="hljs-title function_">eastNorthUpToFixedFrame</span>(<br>    <span class="hljs-title class_">Cesium</span>.<span class="hljs-property">Cartesian3</span>.<span class="hljs-title function_">fromDegrees</span>(-<span class="hljs-number">75.59777</span>, <span class="hljs-number">40.03883</span>)), <span class="hljs-keyword">new</span> <span class="hljs-title class_">Cesium</span>.<span class="hljs-title class_">Cartesian3</span>(<span class="hljs-number">0.0</span>, <span class="hljs-number">0.0</span>, <span class="hljs-number">3000000.0</span>), <span class="hljs-keyword">new</span> <span class="hljs-title class_">Cesium</span>.<span class="hljs-title class_">Matrix4</span>()),<br>  attributes : &#123;<br>    color : <span class="hljs-title class_">Cesium</span>.<span class="hljs-property">ColorGeometryInstanceAttribute</span>.<span class="hljs-title function_">fromColor</span>(<span class="hljs-title class_">Cesium</span>.<span class="hljs-property">Color</span>.<span class="hljs-property">AQUA</span>)<br>  &#125;,<br>  id : <span class="hljs-string">&#x27;top&#x27;</span><br>&#125;);<br></code></pre></td></tr></table></figure>

<h4 id="Combing-Geometries-合并几何图形"><a href="#Combing-Geometries-合并几何图形" class="headerlink" title="Combing Geometries - 合并几何图形"></a>Combing Geometries - 合并几何图形</h4><p>我们可以合并多个Instance为一个Primitive，提高我们的性能。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">var</span> instances = [<span class="hljs-title class_">Instance1</span>, <span class="hljs-title class_">Instance2</span>, <span class="hljs-title class_">Instance3</span>, ……];<br>scene.<span class="hljs-property">primitives</span>.<span class="hljs-title function_">add</span>( <span class="hljs-keyword">new</span> <span class="hljs-title class_">Cesium</span>.<span class="hljs-title class_">Primitive</span>( &#123;<br>    geometryInstances : instances, <span class="hljs-comment">//合并</span><br>    <span class="hljs-comment">//某些外观允许每个几何图形实例分别指定某个属性，例如：</span><br>    appearance : <span class="hljs-keyword">new</span> <span class="hljs-title class_">Cesium</span>.<span class="hljs-title class_">PerInstanceColorAppearance</span>(&#123;translucent : <span class="hljs-literal">false</span>,closed : <span class="hljs-literal">true</span>&#125;)<br>&#125; ) );<br></code></pre></td></tr></table></figure>

<h4 id="更新单个Instance属性"><a href="#更新单个Instance属性" class="headerlink" title="更新单个Instance属性"></a>更新单个Instance属性</h4><p>在添加到Primitive后，我们仍然可以通过Id获取指定Instance并修改其属性。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">var</span> circleInstance = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Cesium</span>.<span class="hljs-title class_">GeometryInstance</span>( &#123;<br>    geometry : <span class="hljs-keyword">new</span> <span class="hljs-title class_">Cesium</span>.<span class="hljs-title class_">CircleGeometry</span>( &#123;<br>        center : <span class="hljs-title class_">Cesium</span>.<span class="hljs-property">Cartesian3</span>.<span class="hljs-title function_">fromDegrees</span>( -<span class="hljs-number">95.0</span>, <span class="hljs-number">43.0</span> ),<br>        radius : <span class="hljs-number">250000.0</span>,<br>        vertexFormat : <span class="hljs-title class_">Cesium</span>.<span class="hljs-property">PerInstanceColorAppearance</span>.<span class="hljs-property">VERTEX_FORMAT</span><br>    &#125; ),<br>    attributes : &#123;<br>        color : <span class="hljs-title class_">Cesium</span>.<span class="hljs-property">ColorGeometryInstanceAttribute</span>.<span class="hljs-title function_">fromColor</span>( <span class="hljs-keyword">new</span> <span class="hljs-title class_">Cesium</span>.<span class="hljs-title class_">Color</span>( <span class="hljs-number">1.0</span>, <span class="hljs-number">0.0</span>, <span class="hljs-number">0.0</span>, <span class="hljs-number">0.5</span> ) ),<br>        show : <span class="hljs-keyword">new</span> <span class="hljs-title class_">Cesium</span>.<span class="hljs-title class_">ShowGeometryInstanceAttribute</span>( <span class="hljs-literal">true</span> ) <span class="hljs-comment">//显示或者隐藏</span><br>    &#125;,<br>    id : <span class="hljs-string">&#x27;circle&#x27;</span><br>&#125; );<br><span class="hljs-keyword">var</span> primitive = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Cesium</span>.<span class="hljs-title class_">Primitive</span>( &#123;<br>    geometryInstances : circleInstance,<br>    appearance : <span class="hljs-keyword">new</span> <span class="hljs-title class_">Cesium</span>.<span class="hljs-title class_">PerInstanceColorAppearance</span>( &#123;<br>        translucent : <span class="hljs-literal">false</span>,<br>        closed : <span class="hljs-literal">true</span><br>    &#125; )<br>&#125; );<br>scene.<span class="hljs-property">primitives</span>.<span class="hljs-title function_">add</span>( primitive );<br><br><span class="hljs-keyword">var</span> attributes = primitive.<span class="hljs-title function_">getGeometryInstanceAttributes</span>( <span class="hljs-string">&#x27;circle&#x27;</span> );<span class="hljs-comment">//获取某个实例的属性集</span><br>attributes.<span class="hljs-property">color</span> = <span class="hljs-title class_">Cesium</span>.<span class="hljs-property">ColorGeometryInstanceAttribute</span>.<span class="hljs-title function_">toValue</span>( <span class="hljs-title class_">Cesium</span>.<span class="hljs-property">Color</span>.<span class="hljs-title function_">fromRandom</span>( &#123;<br>    alpha : <span class="hljs-number">1.0</span><br>&#125; ) );<br></code></pre></td></tr></table></figure>



<h3 id="Appearance"><a href="#Appearance" class="headerlink" title="Appearance"></a>Appearance</h3><p>Cesium支持以下列出的Appearance</p>
<table>
<thead>
<tr>
<th>外观</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>MaterialAppearance</td>
<td>支持各种Geometry类型的外观，支持使用材质来定义着色。支持材料描述阴影。</td>
</tr>
<tr>
<td>EllipsoidSurfaceAppearance</td>
<td>MaterialAppearance的一个版本。假设几何图形与地表是平行的，并且依此来进行顶点属性（vertex attributes）的计算。和Material Appearance一样，就像一个多边形，并且使用这个假设来通过程序上计算许多顶点属性来节省内存。</td>
</tr>
<tr>
<td>PerInstanceColorAppearance</td>
<td>让每个实例使用自定义的颜色来着色，使用每个实例的颜色来遮蔽每个实例。</td>
</tr>
<tr>
<td>PolylineMaterialAppearance</td>
<td>支持使用材质来着色多段线。支持材料遮蔽Polyline。</td>
</tr>
<tr>
<td>PolylineColorAppearance</td>
<td>使用每顶点或者每片段（per-vertex or per-segment ）的颜色来着色多段线—使用每顶点或每段着色来遮蔽折线</td>
</tr>
</tbody></table>
<p>Appearance定义了需要在GPU上执行的GLSL着色器，这部分一般只有在自定义外观时需要修改。</p>
<p><code>render state</code>用来在绘制Primitive的时候控制GPU状态，一旦外观被创建，<code>render state</code>就不能再改变了，但是我们可以修改其材质。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">//下面的外观可用于定义一个Viewer不可进入的不透明盒子</span><br><span class="hljs-keyword">var</span> appearance = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Cesium</span>.<span class="hljs-title class_">PerInstanceColorAppearance</span>( &#123;<br>    translucent : <span class="hljs-literal">false</span>,<br>    closed : <span class="hljs-literal">true</span><br>&#125; );<br><span class="hljs-comment">//下面的代码效果同上</span><br><span class="hljs-keyword">var</span> translucent = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Cesium</span>.<span class="hljs-title class_">PerInstanceColorAppearance</span>( &#123;<br>    renderState : &#123;<br>        depthTest : &#123;<br>            enabled : <span class="hljs-literal">true</span><br>        &#125;,<br>        cull : &#123;<br>            enabled : <span class="hljs-literal">true</span>,<br>            face : <span class="hljs-title class_">Cesium</span>.<span class="hljs-property">CullFace</span>.<span class="hljs-property">BACK</span><br>        &#125;<br>    &#125;<br>&#125; );<br></code></pre></td></tr></table></figure>

<h2 id="Primitive与Entity对比"><a href="#Primitive与Entity对比" class="headerlink" title="Primitive与Entity对比"></a>Primitive与Entity对比</h2><p>看过上边对Primitive相关接口的用法，我们会有这样的疑惑：entity调用方便，封装完美，为什么还要使用Primitive？区别就是记载的效率问题。Primitve更接近webGL的底层，没有entity一样的附加属性，加载时效率会更高。在前文<a target="_blank" rel="noopener" href="https://juejin.cn/post/6972420331982028837">性能优化</a>中已经体现出Primitive的优势了，所以在加载几何体时二者都有优有劣，需要根据具体情况进行选择。</p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/Cesium/" class="category-chain-item">Cesium</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/JavaScript/">#JavaScript</a>
      
        <a href="/tags/Cesium/">#Cesium</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>Cesium | Primitive图元介绍及与Entity对比</div>
      <div>https://moewang0321.github.io/2021/06/17/Cesium中Primitive图元介绍及与Entity对比/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>Moe Wang</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2021年6月17日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/06/21/Cesium%E6%98%AF%E5%A6%82%E4%BD%95%E5%AE%9E%E7%8E%B0%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%9A%84/" title="【三维GIS可视化】基于Vue+Cesium+Supermap实现智慧城市（二）">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">【三维GIS可视化】基于Vue+Cesium+Supermap实现智慧城市（二）</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/06/11/Cesium%E6%B5%B7%E9%87%8F%E7%82%B9%E7%9A%84%E5%8A%A0%E8%BD%BD%E4%B8%8E%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/" title="Cesium | 海量点的加载与性能优化">
                        <span class="hidden-mobile">Cesium | 海量点的加载与性能优化</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
  
  
    <article id="comments" lazyload>
      
    <div id="giscus" class="giscus"></div>
    <script type="text/javascript">
      Fluid.utils.loadComments('#giscus', function() {
        var options = {"repo":"moewang0321/moewang0321.github.io","repo-id":"MDEwOlJlcG9zaXRvcnkyMDEyNDI0NTY=","category":"Show and tell","category-id":"DIC_kwDOC_63WM4CUMsH","theme-light":"light_protanopia","theme-dark":"dark_dimmed","mapping":"pathname","reactions-enabled":1,"emit-metadata":1,"input-position":"top","loading":"lazy","lang":"zh-CN"};
        var attributes = {};
        for (let option in options) {
          if (!option.startsWith('theme-')) {
            var key = option.startsWith('data-') ? option : 'data-' + option;
            attributes[key] = options[option];
          }
        }
        var light = 'light_protanopia';
        var dark = 'dark_dimmed';
        window.GiscusThemeLight = light;
        window.GiscusThemeDark = dark;
        attributes['data-theme'] = document.documentElement.getAttribute('data-user-color-scheme') === 'dark' ? dark : light;
        for (let attribute in attributes) {
          var value = attributes[attribute];
          if (value === undefined || value === null || value === '') {
            delete attributes[attribute];
          }
        }
        var s = document.createElement('script');
        s.setAttribute('src', 'https://giscus.app/client.js');
        s.setAttribute('crossorigin', 'anonymous');
        for (let attribute in attributes) {
          s.setAttribute(attribute, attributes[attribute]);
        }
        var ss = document.getElementsByTagName('script');
        var e = ss.length > 0 ? ss[ss.length - 1] : document.head || document.documentElement;
        e.parentNode.insertBefore(s, e.nextSibling);
      });
    </script>
    <noscript>Please enable JavaScript to view the comments</noscript>


    </article>
  


          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
